/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 学习.上.数组工具类;

/**
 * @description 数组的工具类
 * @param
 * @return
 * @author 俊昭
 * @date 2022/3/16
*/
public class ArrayUtil {
    public static void main(String[] args) {
        ArrayUtil arrayUtil = new ArrayUtil();
        int[] array = new int[]{1,55,23,55,34,12,23,54,23,-34,65,72};
        arrayUtil.Max(array);
        arrayUtil.Min(array);
        arrayUtil.Sum(array);
        arrayUtil.Average(array);

        arrayUtil.ReverseOrder(array);
        arrayUtil.Print(array);

        int[] arr2 = arrayUtil.Copy(array);
        arrayUtil.Print(arr2);

        int[] arr3 = arrayUtil.Sort(array);
        arrayUtil.Print(arr3);

        int[] array2 = new int[]{1,55,23,55,34,12,23,54,23,-34,65,72};
        arrayUtil.Print(array2);
        arrayUtil.Search(array2,55);
        arrayUtil.Search(array2,100);

    }

    //求数组的最大值
    public int Max(int[] arr){
        int temp = arr[0];
        for (int j : arr) {
            if (j > temp) {
                temp = j;
            }
        }
        System.out.println("最大值是：" + temp);
        return temp;
    }
    //求数组的最小值
    public void Min(int[] arr){
        int temp = arr[0];
        for (int j : arr) {
            if (j < temp) {
                temp = j;
            }
        }
        System.out.println("最小值是：" + temp);
    }
    //求数组的和
    public void Sum(int[] arr){
        int temp = 0;
        for (int i:arr){
            temp+=i;
        }
        System.out.println("数组的和是：" + temp);

    }
    //求数组的平均值
    public void Average(int[] arr){
        int temp = 0;
        for (int i:arr){
            temp+=i;
        }
        System.out.println("数组的平均值是："+temp/arr.length);

    }
    //反转数组
    public void ReverseOrder(int[] arr){
        /*if (arr.length%2==0){
            for (int i =0 ;i< arr.length/2;i++){
                arr[i] = arr[arr.length-1-i];
            }
        } else {
            for (int i =0 ;i< arr.length/2;i++){

            }
        错误尝试
        }

         */
        int temp;
        for (int i =0 ;i< arr.length/2;i++) {
            temp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;
        }

    }
    //复制数组
    public int[] Copy(int[] arr){
        int[] temp = new int[arr.length];
        for (int i=0;i< arr.length;i++){
            temp[i] = arr[i];
        }
        return temp;
    }
    //数组排序
    public int[] Sort(int[] arr){
        int temp =0;
        for (int i = 0 ;i< arr.length-1;i++){
            for (int j = 0;j< arr.length-1-i;j++){
                if (arr[j]>arr[j+1]){
                    temp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
    //遍历数组
    public void Print(int[]arr){
        for (int i:arr){
            System.out.print(i+" ");
        }
        System.out.println();
    }
    //查找指定元素
    public void Search(int[] arr,int x){
        boolean flag = false;
        for (int i:arr){
            if (i==x){
                flag = true;
                break;
            }
        }
        if(flag){
            System.out.print("出现该元素的索引是:");
            for (int i=0;i< arr.length-1;i++){
                if (arr[i]==x){
                    System.out.print(i+" ");
                }
            }
            System.out.println();
        }else{
            System.out.println("数组中未查到该元素。");
        }

    }

}
